﻿namespace OpenLightGroup.Common.UserManagement.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Web.Profile;
    using System.Web.Security;
    using System.Linq;


    /// <summary>
    ///   RIA Services Domain Service that exposes methods for performing user
    ///   registrations.
    /// </summary>
    [EnableClientAccess]
    public partial class UserManagementService : DomainService
    {



        [Query]
        public IEnumerable<User> GetUsers()
        {

            var users = from MembershipUser u in Membership.GetAllUsers()
                        select new User()
                        {
                            UserName = u.UserName,
                            Email = u.Email,
                            Roles = Roles.GetRolesForUser(u.UserName)

                        };

            return users;



        }

        public IEnumerable<User> FindUsers(string searchType, string term)
        {
            if (string.IsNullOrEmpty(term))
            {
                term = "";
            }

            //var users = from MembershipUser u in Membership.GetAllUsers()
            //            select new User()
            //            {
            //                UserName = u.UserName,
            //                Email = u.Email,
            //                Roles = Roles.GetRolesForUser(u.UserName)

            //            };

            MembershipUserCollection members;

            if (searchType == "Email")
            {

                members = Membership.FindUsersByEmail(term);
             
            }
            else
            {
                members = Membership.FindUsersByName(term);

            }

            var users = new List<User>();

            foreach (MembershipUser item in members)
            {
                users.Add(new User()
                        {
                            UserName = item.UserName,
                            Email = item.Email,
                            Roles = Roles.GetRolesForUser(item.UserName)

                        });

            }

            return users;

        }

        public void UpdateUser(User user)
        {
            var member = Membership.GetUser(user.UserName);

            member.Email = user.Email;

            Membership.UpdateUser(member);

            UpdateUserRoles(user.UserName, user.Roles);

        }

        [Invoke]
        public void ChangePassword(string username, string password)
        {


            var user = Membership.GetUser(username);

            user.ChangePassword(user.GetPassword(), password);

        }

        [Invoke]
        public bool PasswordRetrievalEnabled()
        {
            return Membership.EnablePasswordRetrieval;
        }

        [Invoke]
        public void EnableOrDisableUser(string username, bool enabled)
        {
            var user = Membership.GetUser(username);

            user.IsApproved = enabled;

        }

    }
}